package cn.hinglo.gameclaw.admin.service;

import cn.hinglo.gameclaw.admin.dao.HtmlDao;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.data.utils.FieldUtils;
import cn.hinglo.gameclaw.data.utils.ResultUtils;
import cn.hinglo.gameclaw.user.bean.Html;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @createTime: 2018/1/3
 * @author: HingLo
 * @description: html5 服务层
 */

@Service
public class HtmlService {
    private final HtmlDao htmlDao;

    @Autowired
    public HtmlService(HtmlDao htmlDao) {
        this.htmlDao = htmlDao;
    }


    /**
     * 查询全部的HTML集合
     *
     * @return 全部的HTML集合
     */
    public Result listService() {
        List<Html> list = htmlDao.selectAll();
        return ResultUtils.success(list);
    }


    /**
     * 添加HTML管理服务层
     *
     * @param html HTML对象
     * @return 添加结果
     */
    public Result addHtmlService(Html html) {
        html.setDate(new Date());
        return htmlDao.insert(html) > 0 ? ResultUtils.success() : ResultUtils.error("添加失败");
    }

    /**
     * 删除Html
     *
     * @param id HTML唯一id
     * @return 删除结果
     */
    public Result deleteHtmlService(String id) {
        //检测Id格式是否符合要求
        return htmlDao.deleteByPrimaryKey(id) > 0 ? ResultUtils.success() : ResultUtils.error("删除失败");
    }

    public String invitationHtmlService(int type) {
        Html html = new Html().setType(type);
        List<Html> list = htmlDao.select(html);
        if (list.isEmpty()) {
            return null;
        } else {
            return FieldUtils.URL + list.get(0).getUrl();
        }

    }

    public Html htmlService(String id) {
        return htmlDao.selectByPrimaryKey(id);


    }
}
